package com.jiayang.hhotel.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jiayang.hhotel.R;
import com.jiayang.hhotel.bean.Constant;
import com.jiayang.hhotel.fragment.BookDinnerSearchFragment;
import com.jiayang.hhotel.util.DateUtil;
import com.jiayang.hhotel.util.IOUtil;
import com.jiayang.hhotel.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

import cn.droidlover.xdroidmvp.base.XFragmentAdapter;
import cn.droidlover.xdroidmvp.mvp.XActivity;

/**
 * Created by Administrator on 2017/5/23 0023.
 * 订餐查询页面
 */

public class BookDinnerSearchActivity extends XActivity implements View.OnClickListener {

    private TextView startDate,endDate;//开始/结束日期控件
    private String stratDateStr,endDateStr;//开始/结束日期字符串

    private ImageView back;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;

    private List<Fragment> fragmentList= new ArrayList<>();
    private XFragmentAdapter adapter;

    @Override
    public void initData(Bundle savedInstanceState) {
        mViewPager= (ViewPager) findViewById(R.id.viewPager);
        mTabLayout= (TabLayout) findViewById(R.id.tabLayout);
        back= (ImageView) findViewById(R.id.tv_back);
        RelativeLayout titleDate= (RelativeLayout) findViewById(R.id.rl_search_date);

        startDate = (TextView) findViewById(R.id.tv_start_time);
        endDate = (TextView) findViewById(R.id.tv_end_time);

        stratDateStr = DateUtil.MillionTime2DateTime(DateUtil.getCurrentMillionTime(),"yyyy-MM-dd");
        endDateStr = DateUtil.MillionTime2DateTime(DateUtil.getCurrentMillionTime()+24*60*60*1000*3,"yyyy-MM-dd");

        startDate.setText(stratDateStr);
        endDate.setText(stratDateStr);

        titleDate.setOnClickListener(this);
        back.setOnClickListener(this);

        fragmentList.clear();
        fragmentList.add(new BookDinnerSearchFragment(0));
        fragmentList.add(new BookDinnerSearchFragment(0));
        fragmentList.add(new BookDinnerSearchFragment(0));
        fragmentList.add(new BookDinnerSearchFragment(0));
        fragmentList.add(new BookDinnerSearchFragment(0));
        fragmentList.add(new BookDinnerSearchFragment(0));

        String[] titles=getResources().getStringArray(R.array.book_dinner_search_tab_list);

        if (adapter == null) {
            adapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
        }
        mViewPager.setAdapter(adapter);
        mViewPager.setOffscreenPageLimit(6);

        mTabLayout.setupWithViewPager(mViewPager);
        mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                LogUtil.showLog(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.layout_reservation_search;
    }

    @Override
    public Object newP() {
        return null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_back://回退
                finish();
                break;
            case R.id.rl_search_date://标题日期选择
                Intent date = new Intent(this,SelDoubleDateActivity.class);
                startActivityForResult(date, Constant.GUEST_ROOM_THING_REQUEST_CODE);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == Constant.GUEST_ROOM_THING_REQUEST_CODE && resultCode == RESULT_OK){
            stratDateStr = IOUtil.getString("dateIn","");
            endDateStr = IOUtil.getString("dateOut","");
            startDate.setText(stratDateStr);
            endDate.setText(endDateStr);
        }
    }
}
